package com.android.customized;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

/**
 * @author 一旦 2015-3-25. 封装此类用于popMenu的显示.
 */
public class PopMenu {
	// **************************************************************************//

	/** 自定义监听器接口，用于将listView的OnItemClickListener监听器传递出去。 */
	public interface OnPopMenuItemClickListener {
		public void onItemClick(int index);
	}

	/** 自定义的监听器接口实例. */
	private OnPopMenuItemClickListener listener;

	// **************************************************************************//

	/** the context. */
	@SuppressWarnings("unused")
	private Context context;
	/** the LayoutInflater */
	private LayoutInflater inflater;

	// **************************************************************************//

	/** popupWindow */
	private PopupWindow popupWindow;
	/** popMenu_listView */
	private ListView popMenuListView;
	/** the menuItems of the popMenu. */
	private ArrayList<String> menuItems;

	// **************************************************************************//

	public PopMenu(Context context, ArrayList<String> menuItems, int pop_menu_list_view_id, Integer pop_menu_list_view_item_id, int width_dimen_id) {
		/** init of context. */
		this.context = context;
		/** get the layoutInflater. */
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		/** bind the menuItems. */
		this.menuItems = menuItems;
		/** load the popMenuListView. */
		popMenuListView = (ListView) inflater.inflate(pop_menu_list_view_id, null);
		/** set the adapter. */
		popMenuListView.setAdapter(new PopMenuAdapter(pop_menu_list_view_item_id));
		/** set this to be the listener. */
		popMenuListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (listener != null) {
					listener.onItemClick(position);
				}
				dismiss();
			}
		});
		/** instance teh popWindow. */
		popupWindow = new PopupWindow(popMenuListView, context.getResources().getDimensionPixelSize(width_dimen_id), LayoutParams.WRAP_CONTENT);
		/** let the return key can close the popmenu. */
		popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

	}

	/**
	 * set listener.
	 */
	public void setOnPopMenuItemClickListener(OnPopMenuItemClickListener listener) {
		this.listener = listener;
	}

	/**
	 * showAsDropDown
	 */
	public void showAsDropDown(View parent, int xoff, int yoff) {
		popupWindow.showAsDropDown(parent, xoff, yoff);
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.update();
	}

	/**
	 * hide the popupmenu.
	 */
	public void dismiss() {
		popupWindow.dismiss();
	}

	/**
	 * PopMenuAdapter
	 */
	private final class PopMenuAdapter extends BaseAdapter {
		/** popmenu item 's layout id. */
		private int popup_menu_list_view_item_id;

		/**
		 * @param popup_menu_list_view_item_id
		 *            TextView.
		 */
		public PopMenuAdapter(int popup_menu_list_view_item_id) {
			super();
			this.popup_menu_list_view_item_id = popup_menu_list_view_item_id;
		}

		@Override
		public int getCount() {
			return menuItems.size();
		}

		@Override
		public Object getItem(int position) {
			return menuItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView textView = (TextView) convertView;
			if (convertView == null) {
				convertView = (TextView) inflater.inflate(popup_menu_list_view_item_id, null);
				textView = (TextView) convertView;
			}
			textView.setText(menuItems.get(position));
			return textView;
		}

	}

}